package com.ys.controller.web.finance;

import com.ys.config.MenuId;
import com.ys.dto.SubGridConfigDto;
import com.ys.entity.finance.TcwWanliPaymentReturn;
import com.ys.entity.finance.TcwWanliPaymentReturnItem;
import com.ys.service.finance.TcwWanliPaymentReturnService;
import com.ys.service.flow.YspTaskService;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.Flow;
import com.ys.vo.W;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/web/finance/tcwWanliPaymentReturn")
public class WebTcwWanliPaymentReturnController {


	@Autowired
	private CommonApiService<TcwWanliPaymentReturn> commonApiService;

	@Autowired
	private YspTaskService yspTaskService;
	@Autowired
	private TcwWanliPaymentReturnService tcwWanliPaymentReturnService;


	/*
	 * 新增
	 */
	@GetMapping("/toAdd")
	public String toAdd(Model model, HttpServletRequest request, HttpServletResponse response,@RequestParam Map<String, Object> params){
		try {
			String bid = commonApiService.getCommonNextBid(MenuId.TCW_WanliPaymentReturn);
			TcwWanliPaymentReturn tcwWanliPaymentReturn = new TcwWanliPaymentReturn()
					.setStatus(1)
					.setWithdrawalMethod(1)
					.setBid(bid)
					.setCreateTime(new Date());

			// 子列表配置信息
			SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MenuId.TCW_WanliPaymentReturn, new ArrayList<Map<String, Object>>());

			commonApiService.toAdd(model, tcwWanliPaymentReturn, MenuId.TCW_WanliPaymentReturn, null, subGridConfig);
			return "finance/tcwWanliPaymentReturn/tcwWanliPaymentReturnAdd";
		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, e.getMessage());
		}
	}

	/*
	 * 编辑
	 */
	@GetMapping("/toEdit")
	public String toEdit(Model model, HttpServletRequest request, HttpServletResponse response,@RequestParam Map<String, Object> params){
		try {
			if (params.get("bid") == null){
				return W.error(model, "单号不能为空");
			}

			String bid = (params.get("bid").toString());

			Map entity = tcwWanliPaymentReturnService.getExtraParam(bid);
			if (entity == null){
				return W.error(model, "万里汇预付款退回不存在!");
			}
			List<Map<String, Object>> sonlist = tcwWanliPaymentReturnService.getItemList(bid);

			// 子列表配置信息
			SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MenuId.TCW_WanliPaymentReturn, sonlist);

			List<SubGridConfigDto.ButtonDto> buttonArr = subGridConfig.getButtonArr();
			List<SubGridConfigDto.ButtonDto> buttonArrNew = new ArrayList<>();
			Integer state = yspTaskService.flowState(bid);
			if (state == Flow.State.FINISH.value){
				subGridConfig.setButtonArr(new ArrayList<>());
			}else if (state == Flow.State.PROCESS.value){
				for (SubGridConfigDto.ButtonDto dto : buttonArr){
					if (dto.getEvent().equals("event-to-zrWanli")){
						buttonArrNew.add(dto);
					}
					if (dto.getEvent().equals("event-to-delete")){
						buttonArrNew.add(dto);
					}
				}
				subGridConfig.setButtonArr(buttonArrNew);
			}


			commonApiService.toEdit(model, Long.valueOf(entity.get("id").toString()), new TcwWanliPaymentReturn(), MenuId.TCW_WanliPaymentReturn, entity, subGridConfig);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return "finance/tcwWanliPaymentReturn/tcwWanliPaymentReturnEdit";
	}

}
